/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.amiee.nicetab.library;

import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;

/**
 * 未读消息提示View,显示小红点或者带有数字的红点:
 * 数字一位,圆
 * 数字两位,圆角矩形,圆角是高度的一半
 * 数字超过两位,显示99+
 */
public class UnreadMsgUtils {
    public static void show(MsgView msgView, int num) {
        if (msgView == null) {
            return;
        }

        DependentLayout.LayoutConfig lp = (DependentLayout.LayoutConfig) msgView.getLayoutConfig();
        float density = msgView.getContext().getResourceManager().getDeviceCapability().screenDensity / 160f;

        msgView.setVisibility(Component.VISIBLE);
        if (num <= 0) { // 圆点,设置默认宽高
            msgView.setStrokeWidth(0);
            msgView.setText("");

            lp.width = (int) (5 * density);
            lp.height = (int) (5 * density);
            msgView.setLayoutConfig(lp);
        } else {
            lp.height = (int) (18 * density);
            if (num > 0 && num < 10) { // 圆
                lp.width = (int) (18 * density);
                msgView.setText(num + "");
            } else if (num > 9 && num < 100) { // 圆角矩形,圆角是高度的一半,设置默认padding
                lp.width = DependentLayout.LayoutConfig.MATCH_CONTENT;
                msgView.setPadding((int) (6 * density), 0, (int) (6 * density), 0);
                msgView.setText(num + "");
            } else { // 数字超过两位,显示99+
                lp.width = DependentLayout.LayoutConfig.MATCH_CONTENT;
                msgView.setPadding((int) (6 * density), 0, (int) (6 * density), 0);
                msgView.setText("99+");
            }
            msgView.setLayoutConfig(lp);
        }
    }

    public static void setSize(MsgView rtv, int size) {
        if (rtv == null) {
            return;
        }
        DependentLayout.LayoutConfig lp = (DependentLayout.LayoutConfig) rtv.getLayoutConfig();
        lp.width = size;
        lp.height = size;
        rtv.setLayoutConfig(lp);
    }
}
